import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @description: MyServlet
 *
 *  web.xml配置形式，当访问请求路径是myservlet，则进入当前类中的方法
 *      <servlet>
 *         <servlet-name>myServletName</servlet-name>
 *         <servlet-class>MyServlet</servlet-class>
 *     </servlet>
 *     <servlet-mapping>
 *         <servlet-name>myServletName</servlet-name>
 *         <url-pattern>/myservlet</url-pattern>
 *     </servlet-mapping>
 *
 *  MyServlet 继承 HttpServlet
 *
 *   GenericServlet (javax.servlet)
 *     HttpServlet (javax.servlet.http)
 *         MyServlet ()
 *         AnnotationServlet ()
 *         ServletTest ()
 *
 * @projectName testweb
 * @author ganquanzhong
 * @date 2020/6/29 14:37
 */
public class MyServlet extends HttpServlet {

    public MyServlet() {
        System.out.println("MyServlet的默认无参构造器...");
    }

    /**
     * A convenience method which can be overridden so that there's no need
     * to call <code>super.init(config)</code>.
     *
     * <p>Instead of overriding {@link #init()}, simply override
     * this method and it will be called by
     * <code>GenericServlet.init(ServletConfig config)</code>.
     * The <code>ServletConfig</code> object can still be retrieved via {@link
     * #getServletConfig}.
     *
     * @throws ServletException if an exception occurs that
     *                          interrupts the servlet's
     *                          normal operation
     */
    @Override
    public void init() throws ServletException {
        super.init();
        System.out.println("MyServlet执行init方法...");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("MyServlet 在处理get（）请求...");
        PrintWriter out = response.getWriter();
        response.setContentType("text/html;charset=utf-8");
        out.println("My Servlet!");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        System.out.println("MyServlet 在处理post（）请求...");
        PrintWriter out = response.getWriter();
        response.setContentType("text/html;charset=utf-8");
        out.println("My Servlet!");
    }
}